/**
 * Copyright 2013-2015 csiisz
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.csii.user.util;

import com.csii.autopoi.poi.excel.entity.ExportParams;
import com.csii.autopoi.web.def.NormalExcelConstants;
import com.csii.autopoi.web.view.MiniAbstractExcelView;
import com.csii.user.model.TreeModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Entity 实体数据对象导出
 * @author csii
 *
 */
@Slf4j
public class PermissionExcelView extends MiniAbstractExcelView {

	public PermissionExcelView() {
		super();
	}
	int y=0;

	@Override
	public void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String codedFileName = model.get(NormalExcelConstants.PARAMS)==null? "临时文件":((ExportParams)model.get(NormalExcelConstants.PARAMS)).getTitle();
		Workbook workbook = new SXSSFWorkbook();
		if(model.get(NormalExcelConstants.DATA_LIST) instanceof List) {
			List<TreeModel> treeList = (List<TreeModel>) model.get(NormalExcelConstants.DATA_LIST);
			Sheet sheet=workbook.createSheet(model.get(NormalExcelConstants.PARAMS)==null? "sheet":((ExportParams)model.get(NormalExcelConstants.PARAMS)).getSheetName());
			y=0;
			writePermission(treeList,0,sheet);
		}else{
			Map<String,List<TreeModel>> treeMap=(Map<String,List<TreeModel>>) model.get(NormalExcelConstants.DATA_LIST);
			for(String k: treeMap.keySet() ) {
				List<TreeModel> treeList = treeMap.get(k);
				Sheet sheet = workbook.createSheet(k);
				y=0;
				writePermission(treeList, 0, sheet);
			}
		}
		if (model.containsKey(NormalExcelConstants.FILE_NAME)) {
			codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME);
		}
		if (workbook instanceof HSSFWorkbook) {
			codedFileName += HSSF;
		} else {
			codedFileName += XSSF;
		}
		if (isIE(request)) {
			codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
		} else {
			codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
		}
		response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
		ServletOutputStream out = response.getOutputStream();
		workbook.write(out);
		out.flush();
	}
	private int writePermission(List<TreeModel> treeModels,int x,Sheet sheet){
		if(treeModels!=null&&!treeModels.isEmpty()) {
			int size=0;
			for (TreeModel treeModel : treeModels) {
				log.info("x:{}  y:{}  name:{}",x,y,treeModel.getTitle());
				Row row = sheet.createRow(y++);
				row.createCell(x).setCellValue(treeModel.getTitle());

				writePermission(treeModel.getChildren(), x+1, sheet);
			}
			return size;
		}
		return 0;
	}
}
